package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DoMainUtils;

public class IweihaiNewsAnalyse implements AnalyseNews {

	//http://www.iweihai.cn/wedding/info/jewelry/2012/06/180565.asp
	//http://house.iweihai.cn/archive.php?aid=116519
	private static final String[] regex={
		"http://www.iweihai.cn/.*/[0-9]*/[0-9]*/[0-9]*.asp",
		"http://www.iweihai.cn/.*/[0-9]*/[0-9]*/[0-9]*.htm",
		"http://www.iweihai.cn/.*/[0-9]*/[0-9]*/[0-9]*.asp",
		"http://.*.iweihai.cn/archive.php\\?aid=[0-9]*",
        "http://club.iweihai.cn/[a-z-a-z-a-z-0-9]*.html"
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		NewsMeta news=new NewsMeta();
		//news=(NewsMeta) new ParseNewsUtils().fixed(urlMeta, news);
//		NewsMeta news=new NewsMeta();

//		String htmltxt=urlMeta.getHtml();
//		Document doc=Jsoup.parse(htmltxt);
//		String title=null;
//		String content=null;
//		Long date=null;
//		title=doc.select("div.title h1").text();
//		content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
//		date=DateUtils.matchDate(doc.select("div.title").text());
//		news.setUrl(url);
//		news.setTitle(title);
//		news.setContent(content);
//		news.setDate(date);
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://club.iweihai.cn/read-htm-tid-18555106.html";
		IweihaiNewsAnalyse test=new IweihaiNewsAnalyse();
		System.out.println(test.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
